package com.photography.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photography.mapper.ScenicSpotMapper;
import com.photography.pojo.ScenicSpot;
import com.photography.service.ScenicSpotService;
import com.photography.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ScenicSpotServiceImpl implements ScenicSpotService {

    @Autowired
    private ScenicSpotMapper scenicSpotMapper;


    @Autowired
    private IdWorker idWorker;

//    @Override
//    public PageResult<ScenicSpot> findAll(int page, int rows) {
//        Page<ScenicSpot> scenicSpotPage = new Page<>(page, rows);
//        EntityWrapper<ScenicSpot> wrapper = new EntityWrapper<>();
//        wrapper.eq("isstartusing ",1).or().eq("isstartusing ",0);
//        List<ScenicSpot> scenicSpots = scenicSpotMapper.selectPage(scenicSpotPage, wrapper);
//        for (ScenicSpot scenicSpot : scenicSpots){
//            Category category = categoryMapper.selectById(scenicSpot.getCategoryid());
//            scenicSpot.setCategory(category);
//        }
//        scenicSpotPage.setRecords(scenicSpots);
//        PageResult<ScenicSpot> scenicSpotPageResult = new PageResult<>();
//        scenicSpotPageResult.setTotal(scenicSpotPage.getTotal());
//        scenicSpotPageResult.setRows(scenicSpotPage.getRecords());
//        return scenicSpotPageResult;
//    }

    @Override
    public ScenicSpot findById(String id) {

        return scenicSpotMapper.selectById(id);
    }

    @Override
    public void addScenicSpot(ScenicSpot scenicSpot) {
        scenicSpot.setId(idWorker.nextId() + "");
        scenicSpot.setCreated(new Date());
        scenicSpot.setUpdated(scenicSpot.getCreated());
        scenicSpotMapper.insert(scenicSpot);
    }

    @Override
    public void updateStatusScenicSpot(String[] ids,int status) {
        for(String id:ids){
            ScenicSpot scenicSpot = scenicSpotMapper.selectById(id);
            if (status == 0){
                scenicSpot.setIsstartusing(0);
            }else if (status == 1){
                scenicSpot.setIsstartusing(1);
            }else if (status == 2){
                scenicSpot.setIsstartusing(2);
            }
            scenicSpotMapper.updateById(scenicSpot);
        }

    }

    @Override
    public void updateScenicSpot(ScenicSpot scenicSpot) {
        scenicSpot.setUpdated(new Date());
       QueryWrapper<ScenicSpot> wrapper = new QueryWrapper<>();
        wrapper.eq("id",scenicSpot.getId());
        scenicSpotMapper.update(scenicSpot,wrapper);
    }
}
